package board;

import java.io.*;
import java.net.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;
import model.*;
import community.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.*;
import util.*;

public class PhotoBoardAction extends DispatchAction {
	
	 public static Vector getPhotoboardMenus(int cono) {
		 Vector<BoardNames> v = new Vector<BoardNames>();
		  
		 String url = "photoboard.do?method=photoList&cono=" + cono + "&pbname=";
		  
		 PhotoBoardDAO pbDAO = new PhotoBoardDAO();
		 String[] bnames = pbDAO.getBoardMenus(cono);
		  
		 for(int i=0;i<bnames.length;i++){
			 BoardNames bns = new BoardNames();
			 bns.setBname(bnames[i]);
			 bns.setUrl(url);
			 v.add(bns);
		 }  
		 return v;
	 }
	
	//Get PhotoBoard List with Search per Community and BoardName
	public ActionForward photoList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		PhotoForm photoForm = (PhotoForm)form;
		
		int cono = 0;
		String pbname = null;
		
		if(!photoForm.getCono().equals("")||photoForm.getCono()!=null){
			cono = Integer.parseInt(request.getParameter("cono"));
			photoForm.setCono(String.valueOf(cono));
			
		}else{
			cono = Integer.parseInt(photoForm.getCono());
			
		}
		
		if(cono<10){
			Menu menu = new Menu();
			menu.setPhotoBoardMenus(this.getPhotoboardMenus(cono));
			request.getSession().setAttribute("menu", menu);
			
		}else{
			CommunityGo.setMemberCommunity(request);
		
		}
		
		if(!photoForm.getPbname().equals("")){
			pbname = URLDecoder.decode(request.getParameter("pbname"), "UTF-8");
			photoForm.setPbname(pbname);
		
		}else{
			pbname = photoForm.getPbname();
		
		}
		
		PhotoBoardDAO pbDAO = new PhotoBoardDAO();
		
		Pager pager = new Pager();
		String selectField = request.getParameter("selectField");
		String searchWord = request.getParameter("searchWord");
		String words = null;
		Vector list = null;
		
		if(selectField!=null){
			if(searchWord==null){
				words = (String)request.getSession().getAttribute("searchWord");
				
			}else{
				words = searchWord;
				request.getSession().setAttribute("searchWord", searchWord);
				
			}
			//Get PhotoBoard List per Community search Title
			if(selectField.equals("title")){
				String title = words;
				long totalRows = pbDAO.getTitleTotalRows(cono, pbname, title);
				
				pager.init(request, totalRows, 8, 5);
				list = pbDAO.getPhotoPageTitle(pager.getCurrentPage(), pager.getRowsPerPage(), cono, pbname, title);
				
			//Get PhotoBoard List per Community search Member
			}else if(selectField.equals("id")){
				
				String member = words;
				long totalRows = 0;
				if(cono<10){
					totalRows = pbDAO.getMemberTotalRows(cono, pbname, member);
				
				}else{
					member = MemberCommunityDAO.getCommunityMid(member, cono);
					totalRows = pbDAO.getMemberTotalRows(cono, pbname, member);
				
				}
				pager.init(request, totalRows, 8, 5);
				
				list = pbDAO.getPhotoPageMember(pager.getCurrentPage(), pager.getRowsPerPage(), cono, pbname, member);
			}
		//Get PhotoBoard List per Community no search
		}else if(searchWord==null||searchWord.equals("")){
			long totalRows = pbDAO.getBnameTotalRows(cono, pbname);
			pager.init(request, totalRows, 8, 5);
			list = pbDAO.getPhotoPageCommunity(pager.getCurrentPage(), pager.getRowsPerPage(), cono, pbname);
			
			request.getSession().setAttribute("searchWord", null);


		}
		
		photoForm.setPhotoList(list);
		
		User user = (User)request.getSession().getAttribute("user");
		String mcnick = MemberCommunityDAO.getCommunityNick(user.getMid(), cono);
		user.setMcnick(mcnick);
		request.getSession().setAttribute("user", user);
		request.getSession().setAttribute("pager", pager);

		return mapping.findForward("photolist");
	}
	
	//Get Main PhotoBoard List of Top 3 (New and Best)
	public ActionForward photoTopList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		PhotoForm photoForm = (PhotoForm)form;
		Vector<PhotoBoard> bestList = new Vector<PhotoBoard>();
		
		int cono = 4;
		String pbname = "포토갤러리";
		
		PhotoBoardDAO pbDAO = new PhotoBoardDAO();
		photoForm.setTopNewList(pbDAO.getTopNewList(cono, pbname, 3));
		photoForm.setTopBestList(pbDAO.getTopBestList(cono, pbname, 3));
		
		long totalRows = pbDAO.getBnameTotalRows(cono, pbname);
		Pager pager = new Pager();
		pager.init(request, totalRows, 8, 5);
		
		return mapping.findForward("photomain");
	}
	
	//Get One of a PhotoBoard
	public ActionForward photoView(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {	
		PhotoForm photoForm = (PhotoForm)form;
		
		int pbno = 0;
		String no = request.getParameter("pbno");		
		if(no!=null) pbno = Integer.parseInt(no);
		else pbno = Integer.parseInt(photoForm.getPbno());
		
		if(photoForm.getRownum()==null){
			photoForm.setRownum("0");
		}
		
		PhotoBoardDAO pbDAO = new PhotoBoardDAO();
		PhotoBoard pb = pbDAO.getPhotoboard(pbno);
		User user = (User)request.getSession().getAttribute("user");
		
		if(!user.getMid().equals(pb.getMid())){
			pbDAO.increaseCounter(pbno);
		}
		
		//get PhotoComment per PhotoBoard Number
		PhotoCommentDAO pcDAO = new PhotoCommentDAO();
		Vector photoComments = null;
		photoComments = pcDAO.getListPerPbno(pbno);
		
		photoForm.setPbno(String.valueOf(pb.getPbno()));
		photoForm.setCono(String.valueOf(pb.getCono()));
		photoForm.setPbname(pb.getPbname());
		photoForm.setPbtitle(pb.getPbtitle());
		photoForm.setPbcontent(pb.getPbcontent());
		photoForm.setPbcounter(String.valueOf(pb.getPbcounter()));
		photoForm.setMid(pb.getMid());
		photoForm.setPbdate(pb.getPbdate());
		photoForm.setPbofn(pb.getPbofn());
		photoForm.setPbsfn(pb.getPbsfn());
		photoForm.setPbfsize(String.valueOf(pb.getPbfsize()));
		photoForm.setPbwidth(String.valueOf(pb.getPbwidth()));
		photoForm.setPbheight(String.valueOf(pb.getPbheight()));
		photoForm.setPhotoComments(photoComments);
		
		int xsize = pb.getPbwidth();
		int ysize = pb.getPbheight();
		int setsize = 500;
		int viewWidth = 0;
		int viewHeight = 0;
		if (xsize > setsize | ysize > setsize){
			if(xsize > ysize){
				viewWidth = setsize;
				viewHeight = ysize * setsize / xsize;
			}else{
				viewWidth = xsize * setsize / ysize;
				viewHeight = setsize;
			}
		}else{
			viewWidth = xsize;
			viewHeight = ysize;
		}
		photoForm.setViewWidth(String.valueOf(viewWidth));
		photoForm.setViewHeight(String.valueOf(viewHeight));
		
		//Get Front PhotoBoard Number and Title
		Vector frontNumTitle = null;
		frontNumTitle = pbDAO.getFrontNumTitle(pb.getCono(), pb.getPbname(), pbno);
		photoForm.setFrontNumTitle(frontNumTitle);
		
		//Get Next PhotoBoard Number and Title
		Vector nextNumTitle = null;
		nextNumTitle = pbDAO.getNextNumTitle(pb.getCono(), pb.getPbname(), pbno);
		photoForm.setNextNumTitle(nextNumTitle);
		
		//Get Community NickName
		if(pb.getCono()>10){
			MemberCommunityDAO mcDAO = new MemberCommunityDAO();			
			String nickName = mcDAO.getCommunityNick(pb.getMid(), pb.getCono());
			photoForm.setNickName(nickName);
		} else {
			Menu menu = new Menu();
			menu.setPhotoBoardMenus(this.getPhotoboardMenus(pb.getCono()));
			request.getSession().setAttribute("menu", menu);
		}
		
		return mapping.findForward("photoview");
	}
	
	public ActionForward photoInsertForm(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {	
		
		String pbname = URLDecoder.decode(request.getParameter("pbname"),"utf-8");
		PhotoForm pf = (PhotoForm)form;
		pf.setPbname(pbname);
		
		return mapping.findForward("photoinsert");
	}
	
	public ActionForward photoInsert(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		PhotoForm photoForm = (PhotoForm)form;
		PhotoBoard pb = new PhotoBoard();
		PhotoBoardDAO pbDAO = new PhotoBoardDAO();
		
		pb.setPbtitle(photoForm.getPbtitle());
		pb.setPbcontent(photoForm.getPbcontent());
		
		String pbofn = photoForm.getFormFile().getFileName();
		
		String pbsfn = null;
		int pbfsize = photoForm.getFormFile().getFileSize();

		if(pbofn.equals("")){
			pbofn = null;
		}else{
			ServletContext sc = request.getSession().getServletContext();
			String fileName = sc.getRealPath("/uploadfiles/" + pbofn);
			InputStream is = photoForm.getFormFile().getInputStream();
			String savedFileName = FileSaver.save(fileName, is);
			pbsfn = savedFileName.substring(savedFileName.lastIndexOf("\\")+1);
		}
		pb.setPbofn(pbofn);
		pb.setPbsfn(pbsfn);
		pb.setPbfsize(pbfsize);
		User user = (User)request.getSession().getAttribute("user");
		pb.setMid(user.getMid());
		
		int pbwidth = Integer.parseInt(photoForm.getPbwidth());
		int pbheight = Integer.parseInt(photoForm.getPbheight());
		pb.setPbwidth(pbwidth);
		pb.setPbheight(pbheight);
		
		int cono = Integer.parseInt(photoForm.getCono());
		String pbname = photoForm.getPbname();
		pb.setCono(cono);
		pb.setPbname(pbname);
		
		if(pbDAO.insert(pb)){
			return photoList(mapping,form,request,response);
		} else {
			return mapping.findForward("photoinsert");
		}

	}
	
	public ActionForward photoUpdateForm(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {	
		PhotoForm photoForm = (PhotoForm)form;
		String pbno = request.getParameter("pbno");
		String rownum = request.getParameter("rownum");
		
		String pbname = null;
		if(!photoForm.getPbname().equals("")){
			pbname = URLDecoder.decode(request.getParameter("pbname"), "UTF-8");
			photoForm.setPbname(pbname);
		}else{
			pbname = photoForm.getPbname();
		}
		
		PhotoBoardDAO pbDAO = new PhotoBoardDAO();
		PhotoBoard pb = pbDAO.getPhotoboard(Integer.parseInt(pbno));
		
		photoForm.setPbno(String.valueOf(pb.getPbno()));
		photoForm.setMid(pb.getMid());
		photoForm.setPbtitle(pb.getPbtitle());
		photoForm.setPbdate(pb.getPbdate());
		photoForm.setPbcontent(pb.getPbcontent());
		photoForm.setPbcounter(String.valueOf(pb.getPbcounter()));
		photoForm.setPbsfn(pb.getPbsfn());
		photoForm.setPbofn(pb.getPbofn());
		photoForm.setPbfsize(String.valueOf(pb.getPbfsize()));
		photoForm.setPbwidth(String.valueOf(pb.getPbwidth()));
		photoForm.setPbheight(String.valueOf(pb.getPbheight()));
		photoForm.setRownum(rownum);
		photoForm.setPbname(pbname);
		photoForm.setNickName(MemberCommunityDAO.getCommunityNick(pb.getMid(), pb.getCono()));
		
		int xsize = pb.getPbwidth();
		int ysize = pb.getPbheight();
		int setsize = 500;
		int viewWidth = 0;
		int viewHeight = 0;
		if (xsize > setsize | ysize > setsize){
			if(xsize > ysize){
				viewWidth = setsize;
				viewHeight = ysize * setsize / xsize;
			}else{
				viewWidth = xsize * setsize / ysize;
				viewHeight = setsize;
			}
		}else{
			viewWidth = xsize;
			viewHeight = ysize;
		}
		photoForm.setViewWidth(String.valueOf(viewWidth));
		photoForm.setViewHeight(String.valueOf(viewHeight));
		
		return mapping.findForward("photoupdate");
	}
	
	public ActionForward photoUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {	
		PhotoForm photoForm = (PhotoForm)form;
		PhotoBoard pb = new PhotoBoard();
		boolean result = false;
		
		if(!isCancelled(request)){
			pb.setPbno(Integer.parseInt(photoForm.getPbno()));
			pb.setPbtitle(photoForm.getPbtitle());
			pb.setPbcontent(photoForm.getPbcontent());
			
			String pbofn = photoForm.getFormFile().getFileName();
			String pbsfn = null;
			int pbfsize = photoForm.getFormFile().getFileSize();
			
			if(pbofn.equals("")){
				pbofn = null;
			}else{
				ServletContext sc = request.getSession().getServletContext();
				String fileName = sc.getRealPath("/uploadfiles/" + pbofn);
				InputStream is = photoForm.getFormFile().getInputStream();
				String savedFileName = FileSaver.save(fileName, is);
				pbsfn = savedFileName.substring(savedFileName.lastIndexOf("\\")+1);
			}
			pb.setPbofn(pbofn);
			pb.setPbsfn(pbsfn);
			pb.setPbfsize(pbfsize);
			pb.setPbwidth(Integer.parseInt(photoForm.getPbwidth()));
			pb.setPbheight(Integer.parseInt(photoForm.getPbheight()));
			pb.setRownum(Integer.parseInt(photoForm.getRownum()));
			
			PhotoBoardDAO pbDAO = new PhotoBoardDAO();
			
			if(pbofn!=null){
				PhotoBoard pb2 = pbDAO.getPhotoboard(Integer.parseInt(photoForm.getPbno()));
				if(pb2.getPbofn()!=null){
					ServletContext sc = request.getSession().getServletContext();
					String pathName = sc.getRealPath("/uploadfiles/" + pb2.getPbsfn());
					File file = new File(pathName);
					file.delete();
				}
				result = pbDAO.updateTextAndFile(pb);
			}else{
				result = pbDAO.updateText(pb);
			}	
		}
		
		if(result){
			return photoView(mapping,form,request,response);
		} else {
			return mapping.findForward("photoupdate");
		}
	}
	
	public ActionForward photoDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String pbno = request.getParameter("pbno");
		String pbname = request.getParameter("pbname");
		String cono = request.getParameter("cono");
		
		PhotoForm pf = (PhotoForm)form;
		pf.setCono(cono);
		pf.setPbname(pbname);

		PhotoBoardDAO pbDAO = new PhotoBoardDAO();
		PhotoBoard pb = pbDAO.getPhotoboard(Integer.parseInt(pbno));
		
		if(pb.getPbofn()!=null){
			ServletContext sc = request.getSession().getServletContext();
			String pathName = sc.getRealPath("/uploadfiles/" + pb.getPbsfn());
			File file = new File(pathName);
			file.delete();
		}
		
		PhotoCommentDAO pcDAO = new PhotoCommentDAO();
		if(pcDAO.getListPerPbno(pb.getPbno()).size()!=0){
			pcDAO.deletePerPbno(Integer.parseInt(pbno));
		}
		
		if(pbDAO.delete(pb)){
			return photoList(mapping,form,request,response);
		} else {
			return mapping.findForward("photoview");
		}
	}
	
	public ActionForward setMenus(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		 
		Menu menu = new Menu();
		
		if(request.getSession().getAttribute("membercommunity")!=null)request.getSession().setAttribute("membercommunity",null);
					
		menu.setPhotoBoardMenus(this.getPhotoboardMenus(4));
		
		request.getSession().setAttribute("menu", menu);
		
		return photoTopList(mapping, form, request, response);
	}
	
	private static void setMenu(int cono, HttpServletRequest request){
	
		if(request.getSession().getAttribute("menu")==null){
			Menu menu = new Menu();
			menu.setPhotoBoardMenus(getPhotoboardMenus(cono));
			request.getSession().setAttribute("menu", menu);			
		}
		
	}
	
}
